package com.webserver.http;


import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *  响应对象
 *  该类的每一个实例用于表示一个响应，每个响应由三部分构成
 *  1：状态行
 *  2：响应头
 *  3：响应正文
 */
public class HttpServletResponse {
    //状态行相关信息
    private int statusCode = 200;
    private String statusReason = "OK";

    //响应头相关信息
    private Map<String,String> headers = new HashMap<>();

    //响应正文相关信息
    private File entity; //响应正文对应的实体文件

    private Socket socket;

    public HttpServletResponse(Socket socket){
        this.socket = socket;
    }


    public void response() throws Exception {
        /*
                http:响应格式
                HTTP/1.1 200 OK(CRLF) 状态行
                Content-Type: text/html(CRLF) 响应头
                Content-Length: 2546(CRLF)(CRLF)
                1011101010101010101...... 响应正文
             */
        //3.1发送状态行
        sendStatusLine();
        //3.2发送响应头
        sendHeaders();
        //3.3发送正文部分
        sendContent();
    }

    /**
     * 发送状态行
     */
    private void sendStatusLine() throws Exception{
        String line = "HTTP/1.1"+" "+statusCode+" "+statusReason;
        println(line);
    }

    /**
     * 发送响应头
     */
    private void sendHeaders() throws IOException {
        Set<Map.Entry<String,String>> entrySet = headers.entrySet(); //将一个个键值对放入一个set集合中
        for (Map.Entry<String,String> e : entrySet) { //遍历entrySet得到一个个键值对
            String key = e.getKey(); //获取e的key也就是Content-Type
            String value = e.getValue(); //获取值也就是entity后缀名对应的类型
            String line = key+": "+value; //把key和value拼接成http协议响应头的格式
            println(line); //将line转化为字节数组，发送给客户端，再发一个回车换行(遵循Http协议)
        }
        //单独发送CRLF表示响应头部分发送完毕！
        println("");
    }

    /**
     * 发送响应正文
     */
    private void sendContent() throws IOException {
            OutputStream out = socket.getOutputStream();
            FileInputStream fis = new FileInputStream(entity);
            byte[] buf = new byte[1024*10];
            int len;
            while ((len = fis.read(buf)) != -1){
                out.write(buf,0,len);
            }
            System.out.println("响应发送完毕!");
        }

    /**
     * 发送一行字符串
     * @param line
     */
    private void println(String line) throws IOException {
        byte[] data = line.getBytes("ISO8859-1");
        OutputStream out = socket.getOutputStream();
        out.write(data);
        out.write(13); //写入回车符
        out.write(10); //写入换行符
    }



    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getEntity() {
        return entity;
    }

    public void setEntity(File entity) {
        this.entity = entity;
    }

    /**
     * 添加一个要发送的响应头
     * @param name
     * @param value
     */
    public void addHeader(String name,String value){
        this.headers.put(name,value);
    }

}

